#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <opencv2/opencv.hpp>
#include "fstream"
#include "dragon2.h"
#include "cubemap.h"
int w = 480, h = 480;
bool leftButtonState;
bool rightButtonState;
cv::Mat posz_cubeface(480*480,3,CV_32FC1);//正z的cubeface的光源坐标
GLint dragonlid;
GLint GenList()
{
    dragonlid = glGenLists(1);
    glNewList(dragonlid, GL_COMPILE);
    //DrawDragon();
    glEndList();
    return dragonlid;
}
void DrawDragonList()
{
    glPushMatrix();
    //glScalef(2*manager->TempscaleFactor, 2*manager->TempscaleFactor, 2*manager->TempscaleFactor);
    glCallList(dragonlid);
    glPopMatrix();
}
void reshape(int width, int height)
{
    if (height==0)										// Prevent A Divide By Zero By
    {
        height=1;										// Making Height Equal One
    }

    glViewport(0,0,w,h);						// Reset The Current Viewport

    glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
    glLoadIdentity();									// Reset The Projection Matrix

    float whRatio = (GLfloat)width/(GLfloat)height;
    gluPerspective(90, whRatio, 1, 1000);

    glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
}
void idle()
{
    glutPostRedisplay();
}
void getFPS()
{
    static int frame = 0, time, timebase = 0;
    static char buffer[256];
    frame++;
    time=glutGet(GLUT_ELAPSED_TIME);
    if (time - timebase > 1000) {
        sprintf(buffer,"FPS:%4.2f ",
            frame*1000.0/(time-timebase));
        timebase = time;
        frame = 0;
    }
    char *c;
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);  // 选择投影矩阵
    glPushMatrix();               // 保存原矩阵
    glLoadIdentity();             // 装入单位矩阵
    glOrtho(0,480,0,480,-1,1);    // 位置正投影
    glMatrixMode(GL_MODELVIEW);   // 选择Modelview矩阵
    glPushMatrix();               // 保存原矩阵
    glLoadIdentity();             // 装入单位矩阵
    glRasterPos2f(10,10);
    for (c=buffer; *c != '\0'; c++) {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
    }
    glMatrixMode(GL_PROJECTION);  // 选择投影矩阵
    glPopMatrix();                // 重置为原保存矩阵
    glMatrixMode(GL_MODELVIEW);   // 选择Modelview矩阵
    glPopMatrix();                // 重置为原保存矩阵
    glEnable(GL_DEPTH_TEST);
}
void redraw()
{
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    //glBindTexture(GL_TEXTURE_2D, 0);
    glClearColor(0   ,   0  ,    0 ,     1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|    GL_STENCIL_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,20,
              0,0, -1,
              0,1,0);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    GLfloat gray[] = {0.8, 0.8, 0.8, 1.0 };
    GLfloat light_pos[] = {-10, 10, 10, 1};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,gray);
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
    glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
    glEnable(GL_LIGHT0);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertices2);
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, 0, normals2);
    // 绘制立方体
    glDrawElements(GL_TRIANGLES, 300000,  GL_UNSIGNED_INT, (GLvoid*)faces2);
    //DrawDragonList();
    getFPS();
    glutSwapBuffers();
}



int main (int argc,  char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE|GLUT_STENCIL);
    glutInitWindowSize(480,480);

    int windowHandle = glutCreateWindow("gltest");
    glewInit();
    glutDisplayFunc(redraw);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);

    GenList();
    glutMainLoop();
}

